package com.csdn.leon.activity;

import java.util.List;

import org.apache.http.Header;

import com.csdn.leon.adapter.BlogDetailAdapter;
import com.csdn.leon.base.BaseActivity;
import com.csdn.leon.model.BlogModel;
import com.csdn.leon.model.CommonList;
import com.csdn.leon.net.CSDNapi;
import com.csdn.leon.util.JsoupUtil;
import com.csdn.leon.util.StringUtils;
import com.csdn.leon.util.URLUtil;
import com.loopj.android.http.TextHttpResponseHandler;
import com.csdn.leon.base.R;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BlogDetailActivity extends BaseActivity implements OnClickListener {

	private String url;
	private ListView listView;
	private BlogDetailAdapter mAdapter;

	private ParserTask mParserTask;
	private TextView leftTv;
	private ImageView leftImg;
	//手指上下滑动时的最小速度
	private static final int YEPEED_MIN = 1000;
	
	//手指向右滑动时的最小距离
	private static final int XDISTANCE_MIN = 120;
	
	//手指上滑或下滑的最小距离
	private static final int YDISTANCE_MIN = 100;
	
	//记录手指按下时的横坐标
	private float xDown;
	
    //记录手指按下时的纵坐标。
    private float yDown;
    //记录手指移动时的横坐标。
    private float xMove;
    //记录手指移动时的纵坐标。
    private float yMove;
    //用于计算手指滑动的速度。
    private VelocityTracker mVelocityTracker;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_blogdetail_layout);
		initView();
	}

	private void initView() {
		url = this.getIntent().getExtras().getString("blog_url");
		listView = (ListView) findViewById(R.id.detail_list);
		mAdapter = new BlogDetailAdapter(BlogDetailActivity.this);
		listView.setAdapter(mAdapter);
		leftTv = (TextView) findViewById(R.id.title_left_text);
		leftTv.setVisibility(View.GONE);
		leftImg = (ImageView) findViewById(R.id.title_left_img);
		leftImg.setVisibility(View.VISIBLE);
		leftImg.setImageResource(R.drawable.actionbar_back_icon_normal);
		leftImg.setOnClickListener(this);
		sendRequestData();
	}

	protected void sendRequestData() {
		showWaitDialog();
		CSDNapi.getBlogList(url, handle);
	}

	TextHttpResponseHandler handle = new TextHttpResponseHandler() {

		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2,
				Throwable arg3) {
			hideWaitDialog();
			Toast.makeText(BlogDetailActivity.this, "加载失败，请重新加载...", 2000)
					.show();
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, String s) {
			Log.w("dd", s);
			executeParserTask(s);

		}
	};

	private void executeParserTask(String data) {
		cancelParserTask();
		mParserTask = new ParserTask(data);
		mParserTask.execute();
	}

	private void cancelParserTask() {
		if (mParserTask != null) {
			mParserTask.cancel(true);
			mParserTask = null;
		}
	}

	class ParserTask extends AsyncTask<Void, Void, String> {

		private final String reponseData;
		private boolean parserError;
		private List<BlogModel> list;

		public ParserTask(String data) {
			this.reponseData = data;
		}

		@Override
		protected String doInBackground(Void... params) {
			try {

				CommonList<BlogModel> commonList = paraseToList(reponseData);

				list = commonList.getList();
			} catch (Exception e) {
				e.printStackTrace();

				parserError = true;
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			mAdapter.setList(list);
			hideWaitDialog();
		}
	}

	public CommonList<BlogModel> paraseToList(String data) {
		CommonList<BlogModel> commonList = new CommonList<BlogModel>();
		List<BlogModel> list = JsoupUtil.getContent(data);
		commonList.setList(list);
		return commonList;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_left_img:
			finish();
			break;
		}
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		createVelocityTracker(ev);
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			xDown = ev.getRawX();
			yDown = ev.getRawY();
			break;

		case MotionEvent.ACTION_MOVE:
			xMove = ev.getRawX();
			yMove = ev.getRawY();
			
			//滑动的距离
			int distanceX = (int) (xMove -xDown);
			int distanceY = (int) (yMove -yDown);
			int ySpeed = getScollVelocity();
			
			if(distanceX > XDISTANCE_MIN && (distanceY < YDISTANCE_MIN && distanceY > -YDISTANCE_MIN)&& ySpeed < YEPEED_MIN){
				finish();
			}
			break;
		case MotionEvent.ACTION_UP:
			recycleVelocityTracker();
			break;
		default:
			break;
		}
		return super.dispatchTouchEvent(ev);
	}
	
	private void createVelocityTracker(MotionEvent event){
		if(mVelocityTracker == null){
			mVelocityTracker = VelocityTracker.obtain();
		}
		mVelocityTracker.addMovement(event);
	}
	
	
	private void recycleVelocityTracker(){
		mVelocityTracker.recycle();
		mVelocityTracker =null;
	}
	
	private int getScollVelocity(){
		mVelocityTracker.computeCurrentVelocity(1000);
		int velocity = (int) mVelocityTracker.getYVelocity();
		return Math.abs(velocity);
	}
	
	
}
